#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
爬虫程序主入口
使用WebScraper类进行网页爬取
"""

from scraper import WebScraper


def main():
    """主函数"""
    scraper = None
    try:
        # 创建爬虫实例
        print("🚀 启动爬虫程序...")
        scraper = WebScraper(headless=False, timeout=10)
        
        # 访问目标网站
        url = "https://om0813.zw7gmc49.work/"
        if scraper.navigate_to(url):
            
            # 获取页面信息
            print("\n📊 获取页面信息...")
            page_info = scraper.get_page_info()
            if page_info:
                print(f"   - 页面标题: {page_info['title']}")
                print(f"   - 链接数量: {page_info['links_count']}")
                print(f"   - 图片数量: {page_info['images_count']}")
            
            # 截取页面截图
            print("\n📸 截取页面截图...")
            scraper.take_screenshot("main_screenshot.png")
            
            # 下载页面图片
            print("\n📥 下载页面图片...")
            downloaded_images = scraper.download_images(max_count=3)
            print(f"   - 成功下载 {len(downloaded_images)} 张图片")
            
            # 保存爬取数据
            print("\n💾 保存爬取数据...")
            if page_info:
                scraper.save_data(page_info, "main_scraped_data.json")
            
            print("\n✅ 爬取完成！")
            
        else:
            print("❌ 无法访问目标网站")
        
    except KeyboardInterrupt:
        print("\n⏹️ 用户中断了程序")
    except Exception as e:
        print(f"\n❌ 程序执行出错: {e}")
    finally:
        if scraper:
            scraper.close()


if __name__ == "__main__":
    main()
